package com.carlinfo.demo.service.impl;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSONObject;
import com.carlinfo.common.util.ConstantFinalUtil;
import com.carlinfo.common.util.PageInfoUtil;
import com.carlinfo.demo.dao.IADynastyDao;
import com.carlinfo.demo.dao.IAKingDao;
import com.carlinfo.demo.dao.impl.ADynastyDaoImpl;
import com.carlinfo.demo.dao.impl.AKingDaoImpl;
import com.carlinfo.demo.pojo.ADynasty;
import com.carlinfo.demo.pojo.AKing;
import com.carlinfo.demo.service.IDemoService;

public class DemoServiceImpl implements IDemoService
{

	private IADynastyDao dynastyDao = new ADynastyDaoImpl();
	private IAKingDao kingDao = new AKingDaoImpl();

	@Override
	public JSONObject saveOneDynastyService(ADynasty dynasty)
	{
		JSONObject resultJSON = new JSONObject();
		/* 調用dao保存 */
		int res = this.dynastyDao.saveOneDao(dynasty);
		if (res > 0)
		{

			resultJSON.put("code", "0");
			resultJSON.put("info", ConstantFinalUtil.INFO_JSON.get("0"));
			;
			JSONObject dataJSON = new JSONObject();
			dataJSON.put("effect", res);
			dataJSON.put("id", dynasty.getId());
			resultJSON.put("data", dataJSON);
		} else
		{
			resultJSON.put("code", "1");
			resultJSON.put("info", ConstantFinalUtil.INFO_JSON.get("1"));
		}
		return resultJSON;
	}

	@Override
	public JSONObject updateOneDynastyService(ADynasty dynasty)
	{
		JSONObject resultJSON = new JSONObject();
		int res = this.dynastyDao.updateOneDao(dynasty);
		if (res > 0)
		{
			resultJSON.put("code", "0");
			resultJSON.put("info", ConstantFinalUtil.INFO_JSON.get("0"));
			JSONObject dataJSON = new JSONObject();
			dataJSON.put("effect", res);
			dataJSON.put("id", dynasty.getId());
			resultJSON.put("data", dataJSON);
		} else
		{
			resultJSON.put("code", "1");
			resultJSON.put("info", ConstantFinalUtil.INFO_JSON.get("1"));
		}
		return resultJSON;

	}

	@Override
	public JSONObject deleteOneDynastyService(Map<String, Object> condMap)
	{
		JSONObject resultJSON = new JSONObject();
		int res = this.dynastyDao.deleteOneDao(condMap);
		if (res > 0)
		{
			resultJSON.put("code", "0");
			resultJSON.put("info", ConstantFinalUtil.INFO_JSON.get("0"));
			JSONObject dataJSON = new JSONObject();
			dataJSON.put("effect", res);
			resultJSON.put("data", dataJSON);
		} else
		{
			resultJSON.put("code", "1");
			resultJSON.put("info", ConstantFinalUtil.INFO_JSON.get("1"));
		}
		return resultJSON;
	}

	@Override
	public ADynasty selectOneDynastyService(Map<String, Object> condMap)
	{
		return this.dynastyDao.selectOneDao(condMap);
	}

	@Override
	public List<ADynasty> selectListDynastyService(PageInfoUtil pageInfoUtil, Map<String, Object> condMap)
	{
		return this.dynastyDao.selectListDao(pageInfoUtil, condMap);
	}

	@Override
	public JSONObject saveOneKingService(AKing king)
	{
		JSONObject resultJSON = new JSONObject();
		/* 调用dao保存 */
		int res = this.kingDao.saveOneDao(king);
		if (res > 0)
		{
			/* code码一定要和config.json中的键一样 */
			resultJSON.put("code", "0");
			resultJSON.put("info", ConstantFinalUtil.INFO_JSON.get("0"));

			/* data的类型是jsonObject */
			JSONObject dataJSON = new JSONObject();
			dataJSON.put("effect", res);
			dataJSON.put("id", king.getId());

			resultJSON.put("data", dataJSON);
		} else
		{
			resultJSON.put("code", "1");
			resultJSON.put("info", ConstantFinalUtil.INFO_JSON.get("1"));
		}
		return resultJSON;
	}

	@Override
	public JSONObject updateOneKingService(AKing king)
	{
		JSONObject resultJSON = new JSONObject();
		/* 调用dao保存 */
		int res = this.kingDao.updateOneDao(king);
		if (res > 0)
		{
			/* code码一定要和config.json中的键一样 */
			resultJSON.put("code", "0");
			resultJSON.put("info", ConstantFinalUtil.INFO_JSON.get("0"));

			/* data的类型是jsonObject */
			JSONObject dataJSON = new JSONObject();
			dataJSON.put("effect", res);
			dataJSON.put("id", king.getId());

			resultJSON.put("data", dataJSON);
		} else
		{
			resultJSON.put("code", "1");
			resultJSON.put("info", ConstantFinalUtil.INFO_JSON.get("1"));
		}
		return resultJSON;
	}

	@Override
	public JSONObject deleteOneKingService(Map<String, Object> condMap)
	{
		JSONObject resultJSON = new JSONObject();
		/* 删除记录 */
		int res = this.kingDao.deleteOneDao(condMap);
		if (res > 0)
		{
			resultJSON.put("code", "0");
			/* 从config.json中取 */
			resultJSON.put("info", ConstantFinalUtil.INFO_JSON.get("0"));

			JSONObject dataJSON = new JSONObject();
			dataJSON.put("effect", res);

			resultJSON.put("data", dataJSON);
		} else
		{
			resultJSON.put("code", "1");
			/* 从config.json中取 */
			resultJSON.put("info", ConstantFinalUtil.INFO_JSON.get("1"));
		}
		return resultJSON;
	}

	@Override
	public AKing selectOneKingService(Map<String, Object> condMap)
	{
		AKing king = this.kingDao.selectOneDao(condMap);
		if ("true".equalsIgnoreCase(condMap.get("extend") + ""))
		{
			/* 专门用来查询关联关系 */
			Map<String, Object> extMap = new HashMap<String, Object>();

			/* 拿着皇上表的朝代id,去查询朝代表 */
			/* 先清理掉 */
			extMap.clear();
			extMap.put("id", king.getDynastyId());
			ADynasty dynasty = this.selectOneDynastyService(extMap);
			king.setDynasty(dynasty);
		}
		return king;

	}

	@Override
	public List<AKing> selectListKingService(PageInfoUtil pageInfoUtil, Map<String, Object> condMap)
	{
		List<AKing> kingList = this.kingDao.selectListDao(pageInfoUtil,condMap);
	 		/* 查询朝代
	 		 * :做一个开关
	 		 *  */
	 		if("true".equalsIgnoreCase(condMap.get("extend") + ""))
	 		{
	 			/* 专门用来查询关联关系 */
	 			Map<String, Object> extMap = new HashMap<String, Object>();
	 			
	 			for (Iterator iterator = kingList.iterator(); iterator.hasNext();)
	 			{
	 				AKing king = (AKing) iterator.next();
	 				
	 				/* 拿着皇上表的朝代id,去查询朝代表 */
	 				/* 先清理掉 */
	 				extMap.clear();
	 				extMap.put("id", king.getDynastyId());
	 				ADynasty dynasty = this.selectOneDynastyService(extMap);
	 				king.setDynasty(dynasty);
	 			}
	 		}
	 		return kingList ; 
	}

}
